package com.线程Thread;
@SuppressWarnings("all")
public class 模拟售票 {  //韩顺平586
    /** 这种会导致超卖；
     * 运行后剩余票数正确，但实际售出票超过了100 ；
     *
     */
    public static void main(String[] args) throws InterruptedException {
        Ticket ticket = new Ticket();
        Thread thread1 = new Thread(ticket);
        Thread thread2 = new Thread(ticket);
        Thread thread3 = new Thread(ticket);
        thread1.start();
        thread2.start();
        thread3.start();

        Thread.sleep(3000);
        System.out.println(ticket.sum);


    }
}

class Ticket implements Runnable {
    int tickets = 100;
    int sum = 0;

    @Override
    public void run() {
        while(true) {
            if (tickets <= 0)
                return;

            tickets--;
            System.out.println("卖出一张票，剩余" + tickets);
            try {
                Thread.sleep(50);
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            sum++;

        }

    }
}